
<!--<link rel="stylesheet" href="${request.contextPath}/lib/plugins/zTree3/css/zTreeStyle/zTreeStyle.css">-->
<!--<link rel="stylesheet" href="${request.contextPath}/lib/plugins/zTree3/css/zTreeStyle/ztreeTable.css">-->

<style>
.w1 {	width: 150px;	text-align: center;}
.w2 {	width: 200px;	text-align: center;}
.w3 {	width: 120px;	text-align: center;}
</style>

<div class="box box-primary">
	<div class="box-body">		
		<!-- <div class="row">
			<div class="rowtoolbar">
			<input type="text" value="soa" id="menuType" style="visibility:hidden">  
			    <div class="col-xs-6" > 
			    
				<button type="button" class="btn btn-success" id="add">
				<i class="fa fa-plus-circle fz"></i> 新增</button>
				<button type="button" class="btn btn-warning" disabled="disabled" id="edit">
				<i class="glyphicon glyphicon-pencil"></i> 编辑</button>
				
				<button type="button" class="btn btn-primary" disabled="disabled" id="down">
				<i class="fa fa-arrow-down"></i> 下移</button>
				<button type="button" class="btn btn-primary" disabled="disabled" id="up">
				<i class="fa fa-arrow-up"></i> 上移</button>   
				
				<button type="button" class="btn btn-primary" id="senceTransform" sence="soa">
				<i class="fa fa-rotate-left"></i> 场景切换</button>   				

				<button type="button" class="btn btn-danger" disabled="disabled" id="del">
				<i class="fa fa-trash"></i> 删除</button>
				
				<button type="button" class="btn btn-primary" id="write">
				<i class="fa fa-pencil"></i> 写入缓存</button>
				</div>
			</div>
		</div> -->
		
		<div class="box-header with-border">
				  <div class="box-body" style="width: 100%">
					<div class="row">
						<#list btnlist as items>
							<button class="${items.funcBg!}" id="${items.funcCode!}">
							<i class="${items.funcIcon!}"></i> ${items.funcName!}</button>
						
						</#list>
						<!--<button type="button" class="btn btn-primary" id="senceTransform" sence="soa">-->
				        <!--<i class="fa fa-rotate-left"></i> 场景切换</button>  -->
					</div>
				</div>	
				</div>
		</div>
		<div class="box-body">
		<div class="row">
		<div class="rowtoolbar">
			<div class="rowTitle">
				<span class="t">菜单名称</span> <span class="wr"> 
				<span class="w2"><span class="tfield">菜单路径</span></span>
				<span class="w1"><span class="tfield">是否开放</span></span>

				</span>
			</div>
			<div class="zTreeDemoBackground">
				<ul id="treeGride" class="ztree"></ul>
			</div>
		</div>
		</div>
       </div>
	
</div>
<div class="modal fade bs-example-modal-lg" id="myModal" role="dialog" aria-labelledby="myModalLabel"
	 aria-hidden="true">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabel">正在加载...</h4>
			</div>
			<div class="modal-body">正在加载...</div>
			<div class="modal-footer" id="modal-footer1">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button id="myModalSave" type="button" class="btn btn-primary">保存</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
<!--<script type="text/javascript" src="${request.contextPath}/js/aboutForm.js"></script>-->
<!--<script src="${request.contextPath}/lib/plugins/zTree3/js/jquery.ztree.core.js"></script>-->
<!--<script src="${request.contextPath}/lib/plugins/zTree3/js/jquery.ztree.excheck.js"></script>-->
<!--<script src="${request.contextPath}/lib/plugins/zTree3/js/jquery.ztree.exedit.js"></script>-->
<script type="text/javascript">




/* kxdtreegride
 *
 *
 */
kxdtreegrid = {};

var kxdgride=null;
var treeObj=null;
var showdialog=null;
/* --------------------
 * 
 * --------------------
 * 
 */

kxdtreegrid.options = {
	curMenu : null, 
	zTree_Menu : null,
	 setting : {
		  view: {
		    showLine: false,
		    showIcon: false,
		    selectedMulti: false,
		    dblClickExpand: false,
		    addDiyDom: addDiyDom
		  },
		  data: {
			  simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "parentId",
					rootPId: 0
				},
		  		key:{
		  			name:"menuName",
		  			menuUrl:"menuUrl",
		  			isopen:"menuIsOpen"
		  		}
		  },
		  async: {
				enable: true,
				type: "post",
				url: contextPath+"/soa/menu/datas/search2",
				autoParam: ["id"],
				dataFilter: ajaxDataFilter
			},
		  callback: {
		    beforeClick: beforeClick,
		    onClick:zTreeOnClick,
		    onAsyncSuccess: onAsyncSuccess,
		    beforeAsync: zTreeBeforeAsync
		    
		  },		  		
	},
};

function showdialogfn(obj){
	showdialog=dialog({
		okValue : obj,
		ok : function() {			
		}
  	});
  	showdialog.show();
}

function hiddialog(){
	showdialog.okValue="";
  	showdialog.close().remove();
  }

function addDiyDom(treeId, treeNode) {
    var spaceWidth = 10;
    var switchObj = $("#" + treeNode.tId + "_switch"),
    icoObj = $("#" + treeNode.tId + "_ico");
    
    switchObj.remove();
    icoObj.before(switchObj);

    if (treeNode.level > 0) {
      var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
      switchObj.before(spaceStr);
    }

    addcon(treeId, treeNode);
    
  };
  
  function addcon(treeId, treeNode){
      aObj = $("#" + treeNode.tId + "_a");
      var $wr = $('<span>').addClass('wr');
      var $s1 = $('<span>').addClass('w2').html(treeNode.menuUrl);
//      var $s2 = $('<span>').addClass('w1').html("");
      var $s3 = $('<span>').addClass('w1').html(treeNode.isopen<0?"内部地址":"开放地址");
      //var $s4 = $('<span>').addClass('w1').html(treeNode.menuOrder);
      //var $s5 = $('<span>').addClass('w1').html(treeNode.lastModifiedTime);

      $s1.appendTo($wr);
//      $s2.appendTo($wr);
      $s3.appendTo($wr);
     //$s4.appendTo($wr);
     // $s5.appendTo($wr);
      $wr.prependTo(aObj);
    };
    
    function beforeClick(treeId, treeNode) {
        return true;
      };
      
	function zTreeOnClick(event, treeId, treeNode) {
		if(treeNode.getParentNode()!=null && treeNode.getParentNode().children.length>1){
			
			if(treeNode.getIndex()==0){
				$('#up').attr("disabled",true);
			}else{
				$('#up').removeAttr('disabled');
			}
			
			if((treeNode.getParentNode().children.length-treeNode.getIndex())>1){
				$('#down').removeAttr('disabled');
			}else{
				$('#down').attr("disabled",true);
			}
			
		}else{
			$('#down').attr("disabled",true);
			$('#up').attr("disabled",true);
		}
		
		
	  var $edit = $('#edit');
	  var $del = $('#del');
	  $edit.removeAttr('disabled');
	  $del.removeAttr('disabled');
	};
	
	function onAsyncSuccess(event, treeId, treeNode, msg){
		  var $edit = $('#edit').attr("disabled",true);
		  var $del = $('#del').attr("disabled",true);
		  //hiddialog();
	}
	
	function zTreeBeforeAsync(treeId, treeNode) {
		//showdialogfn("刷新数据中。。。");
	}
	
	function treeRefresh(node){
		
		if ((node.getParentNode().children.length-1)===0) {
			treeObj.reAsyncChildNodes(node.getParentNode().getParentNode(),"refresh",true);
		}else{
			treeObj.reAsyncChildNodes(node.getParentNode(),"refresh",true);
		}
	}	
	
function _init() {
	var jtreeObj = $("#treeGride");
	kxdgride=$.fn.zTree.init(jtreeObj, kxdtreegrid.options.setting,null);
	treeObj = $.fn.zTree.getZTreeObj("treeGride");
	jtreeObj.hover(function () {
      if (!jtreeObj.hasClass("showIcon")) {
    	  jtreeObj.addClass("showIcon");
      }
    }, function() {
    	jtreeObj.removeClass("showIcon");
    });
}

function ajaxDataFilter(treeId, parentNode, responseData) {
	console.log("注意这里：");
	console.log(responseData);
    if (responseData.data.length>0) {
    	responseData=responseData.data;
    }
    return responseData;
};

_init();

//修改
$('#edit').on('click',function(){
   treeObj = $.fn.zTree.getZTreeObj("treeGride");
   var nodes = treeObj.getSelectedNodes();
   if(parent==null){
		   var d = dialog({
				content : '选择正确的节点添加！'
			});
			d.show();
			setTimeout(function() {
			d.close().remove();
		}, 3000); 
	  }else{
		  //var id=nodes[0].id;
		  var data={"id":nodes[0].id};
		  post("/soa/menu/edit","html",data,function(obj){
				var d = dialog({
					title : '修改菜单',
					content :obj,					
					width : 550,
					height : 500,
					okValue: '保存',
					zIndex:1080,
					ok:function() {
						this.title('正在提交..');
						ajaxSubmit($('#form_menu')[0], function(data){
							if(data.status==0){
								var d = dialog({
			    					content : data.msg
			    				});
			    					d.show();
			    					setTimeout(function() {
			    					d.close().remove();
			    				}, 4000); 
			    				Refresh();//刷新缓存
							}else{
								var d = dialog({
			    					content : data.msg
			    				});
			    					d.show();
			    					setTimeout(function() {
			    					d.close().remove();
			    				}, 3000);
							}
							
						});
					 	cancel: false
					},
			     	cancelValue:'取消',
				 	cancel: function(){
				  	}
				});
				d.showModal();
			});
	  }
});

//隐藏之前清除数据
$("#myModal").on("hidden.bs.modal", function () {
	// console.log("清除数据");
	// $("#myModal").removeData("bs.modal");
	$("#myModal").find(".modal-body").empty();
	$("#myModalSave").off('click');//移除点击事件
});
//配置功能
$('#manageButton').on('click',function(){
	treeObj = $.fn.zTree.getZTreeObj("treeGride");
	var nodes = treeObj.getSelectedNodes();
	if(parent==null){
		var d = dialog({
			content : '选择正确的节点添加！'
		});
		d.show();
		setTimeout(function() {
			d.close().remove();
		}, 3000);
	}else{
		//var id=nodes[0].id;
		var data={"id":nodes[0].id};

		var modal = $('#myModal');
		modal.find('.modal-title').text("配置菜单功能");
		modal.find('.modal-body').load(contextPath + "/soa/menu/findFunces", data, function (obj) {
			 console.log("进入了后台");
		});
		$('#myModal').modal({
			backdrop: false
		});
		$("#myModalSave").on('click', function () {

			ajaxSubmit($('#form_menu_funcs')[0], function (data) {
				console.log("保存完了");
				if(data.status == 0){
					info(data.msg);
					$('#myModal').modal('hide');
//					DTrefresh();
				}else{
					warning(data.msg);
				}
			});

		});
	}
});

var dialogEdit;
var nodeEdit ;
 //添加
 $('#add').on('click',function(){
   treeObj = $.fn.zTree.getZTreeObj("treeGride");
   var nodes = treeObj.getSelectedNodes();
   if(nodes==""){
		  var data={"parentId":0,"tier":0,"code":100000,"sence":"soa"};
		  post("/soa/menu/inst","html",data,function(obj){
				var d = dialog({
					title : '添加菜单',
					content :obj,					
					width : 550,
					height : 500,
					okValue: '保存',
					zIndex:1080,
					ok:function() {
						this.title('正在提交..');
						ajaxSubmit($('#form_menu')[0], function(data){
							if(data.status==0){
							var d = dialog({
	   						content : data.msg
	   					});
	   						d.show();
	   						setTimeout(function() {
	   						d.close().remove();
	   					}, 3000); 
	   					Refresh();//刷新缓存
						}else{
							var d = dialog({
	   						content : data.msg
	   					});
	   						d.show();
	   						setTimeout(function() {
	   						d.close().remove();
	   					}, 3000);
						} 
						});
					 	cancel: false
					},
			     	cancelValue:'取消',
				 	cancel: function(){
				  	}
				});
				d.showModal();
			});
	  }else{
		  //var data={pid:nodes[0].id};
		  var data={"parentId":nodes[0].id,"tier":0,"code":100000,"sence":"soa"};
		 // console.log("parentId:"+nodes[0].id);
		  post("/soa/menu/inst","html",data,function(obj){
				var d = dialog({
					title : '添加菜单',
					content :obj,					
					width : 550,
					height : 500,
					okValue: '保存',
					zIndex:1080,
					ok:function() {
						this.title('正在提交..');
						ajaxSubmit($('#form_menu')[0], function(data){
							if(data.status==0){
							var d = dialog({
	   						content : data.msg
	   					});
	   						d.show();
	   						setTimeout(function() {
	   						d.close().remove();
	   					}, 3000); 
	   					Refresh();//刷新缓存
						}else{
							var d = dialog({
	   						content : data.msg
	   					});
	   						d.show();
	   						setTimeout(function() {
	   						d.close().remove();
	   					}, 3000);
						} 
						});
					 	cancel: false
					},
			     	cancelValue:'取消',
				 	cancel: function(){
				  	}
				});
				d.showModal();
			});
		  
	  }
});

 //删除
 $('#del').on('click',function(){
     var nodes = treeObj.getSelectedNodes();
     if(nodes==""){
		  var d = dialog({
	            title: '提示',
	            content:'请选择节点删除！',
	            okValue: '确定',
	            ok: function() {
	            	setTimeout(function() {
  						d.close().remove();
  					}, 3000); 
	            },
	            cancelValue: '取消',
	            cancel: function() {}
	         });
	      d.show();
	  }else{
		var id=nodes[0].id;
		var sence=$("#menuType").val();
		var pid=nodes[0].parentId ;
		var order= nodes[0].menuOrder;
		var tier= nodes[0].level;
		var code= nodes[0].menuCode;
		console.log(nodes[0]);
		//console.log("id:"+id+",sence:"+sence+",pid:"+pid+",order:"+order+",tier:"+tier+",code:"+code);
	      var d = dialog({
	          title:'提示',
	          content:'确认删除菜单名为：'+nodes[0].menuName+' 的这条记录？',
	          okValue:'确认',
	          ok:function(){
	        	var stat=false;
	          	$.ajax({
	          		async: false,
	          		url:contextPath+"/soa/menu/datas/del2",
	          		data: {id:id,pid:pid,order:order,tier:tier,sence:sence},
	          		type:"post",
	          		dataType:"json",
	          		success: function(result){
	          			if(result.status==0){
	          				var d = dialog({
	              				content : result.msg
	              			});
	              			d.show();
	              			setTimeout(function() {
	              				d.close().remove();
	              			}, 3000);
	              			stat=true;
	          			}else{
	          				var d = dialog({
	              				content : result.msg
	              			});
	              			d.show();
	              			setTimeout(function() {
	              				d.close().remove();
	              			}, 3000);
	              			stat=false;
	          			}
	
	          		},
	          		});
	          	
	          	if(stat == true){
	          		d.close().remove();
	          		Refresh()
	          	}
	          	
	         	
	          },
	          cancelValue:'取消',
	          cancel:function(){}
			  
			});
			d.showModal(); 
	      
	  }
 });
 
 
 $('#up').on('click',function position(){
	 treeObj = $.fn.zTree.getZTreeObj("treeGride");
     var nodes = treeObj.getSelectedNodes();
     if(nodes==""){
		  var d = dialog({
	            title: '提示',
	            content:'请选择节点进行修改！',
	            okValue: '确定',
	            ok: function() {
	            	setTimeout(function() {
  						d.close().remove();
  					}, 2000); 
	            },
	            cancelValue: '取消',
	            cancel: function() {}
	         });
	      d.show();
	  }else{
		  var id=nodes[0].id;
		  var nid=nodes[0].getPreNode().id;
		var data = {
				"cmd":0,
				"id":id,
				"nid":nid
			};
     	$.ajax({
     		url:contextPath+"/soa/menu/datas/position",    
     		data:data,
     		type:"post",
     		dataType:"json",
     		async:false,
     		success: function(result){
     			if(result.status==0){
     				treeRefresh(nodes[0]);
     				var d = dialog({
 						content : result.msg
 					});
 						d.show();
 						setTimeout(function() {
 						d.close().remove();
 					}, 2000); 
					}else{
						var d = dialog({
 						content : result.msg
 					});
 						d.show();
 						setTimeout(function() {
 						d.close().remove();
 					}, 2000);
					} 
     		} 
     	});
	  }
  });
 
 
 $('#down').on('click',function position(){
	 treeObj = $.fn.zTree.getZTreeObj("treeGride");
     var nodes = treeObj.getSelectedNodes();
     if(nodes==""){
		  var d = dialog({
	            title: '提示',
	            content:'请选择节点进行修改！',
	            okValue: '确定',
	            ok: function() {
	            	setTimeout(function() {
  						d.close().remove();
  					}, 2000); 
	            },
	            cancelValue: '取消',
	            cancel: function() {}
	         });
	      d.show();
	  }else{
		  var id=nodes[0].id;
		  var nid=nodes[0].getNextNode().id;
		
		var data = {
				"cmd":1,
				"id":id,
				"nid":nid
			};
     	$.ajax({
     		url:contextPath+"/soa/menu/datas/position",    
     		data:data,
     		type:"post",
     		dataType:"json",
     		async:false,
     		success: function(result){
     			if(result.status==0){
     				treeRefresh(nodes[0]);
     				var d = dialog({
 						content : result.msg
 					});
 						d.show();
 						setTimeout(function() {
 						d.close().remove();
 					}, 2000); 
					}else{
						var d = dialog({
 						content : result.msg
 					});
 						d.show();
 						setTimeout(function() {
 						d.close().remove();
 					}, 2000);
					} 
     		} 
     	});
	  }
  });
 
 
 //写缓存
 $('#writeCahe').on('click',function(){
	 var sence = $("#menuType").val();
	          	$.ajax({
	          		async: false,
	          		url:contextPath+"/soa/menu/writeCache",
	          		data: {'sence':sence},
	          		type:"post",
	          		dataType:"json",
	          		success: function(obj){	        			
	          				var d = dialog({
	              				content : obj.msg
	              			});
	              			d.show();
	              			setTimeout(function() {
	              				d.close().remove();
	              			}, 3000);
	              	     },
	          		});
	  
                });
 
</script>
<script type="text/javascript">

var sence=$("#senceTransform").attr("sence");
//场景切换
$("#senceTransform").on("click",function(){
	if(sence=="soa"){
		$("#senceTransform").attr("sence","coa");
		sence=$("#senceTransform").attr("sence");
		//这句话是重新定义setting里的url值，达到再次init、
		kxdtreegrid.options.setting.async.url=contextPath+"/soa/menu/datas/search2?sence=coa";
	}else if(sence=="coa"){
		$("#senceTransform").attr("sence","soa");
		sence=$("#senceTransform").attr("sence");
		//这句话是重新定义setting里的url值，达到再次init、
		kxdtreegrid.options.setting.async.url=contextPath+"/soa/menu/datas/search2?sence=soa";
	}
	//弹出对话框，源码在aboutForm.js
	showDailog('切换成功，请稍后！',2000);
	_init();
	
});


function Refresh() {
	 
	     var sence = $("#menuType").val();
	     var sence="soa";
		
		$.ajax({
   		url:contextPath+"/soa/manage/datas/menurefresh",
   		data:{"sence":sence},
   		type:"post",
   		dataType:"json",
   		success: function(result){
   			_init();
   		    } 
   		});
	} 
</script>
